﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WCS.Infrastructure.Dto.Response.Payment
{
    /// <summary>
    /// 分账记录响应对象
    /// </summary>
    public class DividedRecordResponseDto
    {
        /// <summary>
        /// 分账记录ID
        /// </summary>
        public string? Id { get; set; }

        /// <summary>
        /// 分账记录编号
        /// </summary>
        public string? RecordNo { get; set; }

        /// <summary>
        /// 关联订单ID
        /// </summary>
        public string? OrderId { get; set; }

        /// <summary>
        /// 收款码ID
        /// </summary>
        public string? CodeId { get; set; }

        /// <summary>
        /// 分账协议ID
        /// </summary>
        public string? DividedAgreementId { get; set; }

        /// <summary>
        /// 分账协议名称
        /// </summary>
        public string? DividedAgreementName { get; set; }

        /// <summary>
        /// 分账总金额
        /// </summary>
        public long? TotalDividedAmount { get; set; }

        /// <summary>
        /// 商户ID
        /// </summary>
        public string? MerchantId { get; set; }

        /// <summary>
        /// 门店ID
        /// </summary>
        public string? StoreId { get; set; }

        /// <summary>
        /// 分账日期
        /// </summary>
        public DateTime DividedDate { get; set; }

        /// <summary>
        /// 扩展属性
        /// </summary>
        public string? ExtraProperties { get; set; }

        /// <summary>
        /// 并发标记
        /// </summary>
        public string? ConcurrencyStamp { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreationTime { get; set; }

        /// <summary>
        /// 创建者ID
        /// </summary>
        public string? CreatorId { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? LastModificationTime { get; set; }

        /// <summary>
        /// 修改者ID
        /// </summary>
        public string? LastModifierId { get; set; }

        /// <summary>
        /// 用户分账金额
        /// </summary>
        public long? UserDividedAmount { get; set; }

        /// <summary>
        /// 入账时间
        /// </summary>
        public DateTime? MaxEntryTime { get; set; }
    }

    /// <summary>
    /// 分账记录汇总响应对象
    /// </summary>
    public class DividedRecordSumResponseDto
    {
        /// <summary>
        ///  查询日期
        /// </summary>
        public DateTime? QueryDate { get; set; }

        /// <summary>
        /// 总金额
        /// </summary>
        public long TotalAmount { get; set; } = 0;

        /// <summary>
        /// 总笔数
        /// </summary>
        public int TotalCount { get; set; } = 0;

        /// <summary>
        /// 
        /// </summary>
        public List<DividedRecordResponseDto> DividedRecordList { get; set; } = new List<DividedRecordResponseDto>();
    }

    /// <summary>
    /// 分账记录详情响应对象
    /// </summary>
    public class DividedRecordDetailResponseDto
    {
        /// <summary>
        /// 分账记录详情ID
        /// </summary>
        public string? Id { get; set; }

        /// <summary>
        /// 分账记录ID
        /// </summary>
        public string? DividedRecordId { get; set; }

        /// <summary>
        /// 分账接收方用户ID
        /// </summary>
        public string? UserId { get; set; }

        /// <summary>
        /// 用户名称
        /// </summary>
        public string? UserName{ get; set; }

        /// <summary>
        /// 分账比例
        /// </summary>
        public decimal? Ratio { get; set; }

        /// <summary>
        /// 分账金额
        /// </summary>
        public long? DividedAmount { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreationTime { get; set; }

        /// <summary>
        /// 入账时间
        /// </summary>
        public DateTime? EntryTime { get; set; }

        /// <summary>
        /// 是否入账
        /// </summary>
        public bool? IsEntry { get; set; }

        /// <summary>
        /// 是否是主账户
        /// </summary>
        public bool? IsMasterAccount { get; set; }


    }


    /// <summary>
    /// 路径节点（用于记录每一层的协议、用户与比例）
    /// </summary>
    public class PathNode
    {
        /// <summary>
        /// 协议ID
        /// </summary>
        public string AgreementId { get; set; } = default!;

        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserId { get; set; } = default!;

        /// <summary>
        /// 比例
        /// </summary>
        public decimal Ratio { get; set; }
    }

    /// <summary>
    /// 叶子拆分结果（用于生成分账详情）
    /// </summary>
    public class LeafSplit
    {
        /// <summary>
        /// 叶子参与人用户ID（最终收款方）
        /// </summary>
        public string LeafUserId { get; set; } = default!;

        /// <summary>
        /// 从根到叶子的比例累乘（用于计算金额）
        /// </summary>
        public decimal RatioProduct { get; set; }

        /// <summary>
        /// 叶子所在层的协议ID（最后一个协议）
        /// </summary>
        public string LayerAgreementId { get; set; } = default!;

        /// <summary>
        /// 从根到叶子的拆分路径（包含每层节点）
        /// </summary>
        public List<PathNode> Path { get; set; } = new();
    }

  
    /// <summary>
    /// 协议树中的协议节点（用于前端树形展示）
    /// </summary>
    public class AgreementTreeNode
    {
        /// <summary>
        /// 协议ID（当前层对应的分账协议）
        /// </summary>
        public string AgreementId { get; set; } = "";

        /// <summary>
        /// 协议名称（用于前端展示）
        /// </summary>
        public string? AgreementName { get; set; }

        /// <summary>
        /// 从根到当前层的累计比例（AccRatio = 上一层 AccRatio * 路径节点比例）
        /// </summary>
        public decimal AccRatio { get; set; }

        /// <summary>
        /// 当前层累计金额（AccAmount = TotalDividedAmount * AccRatio）
        /// </summary>
        public long AccAmount { get; set; }

        /// <summary>
        /// 当前层参与者列表（每个参与者包含比例、金额及子层信息）
        /// </summary>
        public List<ParticipantTreeNode> Participants { get; set; } = new();
    }

    /// <summary>
    /// 参与者节点（用于前端树形展示）
    /// </summary>
    public class ParticipantTreeNode
    {
        /// <summary>
        /// 参与者用户ID
        /// </summary>
        public string UserId { get; set; } = "";

        /// <summary>
        /// 参与者姓名
        /// </summary>
        public string? UserName { get; set; }

        /// <summary>
        /// 参与者手机号
        /// </summary>
        public string? Phone { get; set; }

        /// <summary>
        /// 参与者在本层的静态比例（来源于分账记录详情 PathJson 快照）非路径参与者比例为 0
        /// </summary>
        public decimal Ratio { get; set; }

        /// <summary>
        /// 参与者在本层的金额（Amount = TotalDividedAmount * 当前层 AccRatio * 本层 Ratio）非路径参与者金额为 0
        /// </summary>
        public long Amount { get; set; }

        /// <summary>
        /// 是否存在子分账协议（用于前端展示可展开标识）
        /// </summary>
        public bool HasSubAgreement { get; set; }

        /// <summary>
        /// 子分账协议ID（若存在）
        /// </summary>
        public string? SubAgreementId { get; set; }

        /// <summary>
        /// 标记该参与者的子层中间存在被省略的层级（用于前端提示）
        /// </summary>
        public bool HasOmittedSubLayers { get; set; }

        /// <summary>
        /// 子层节点（若存在子协议，递归生成的下一层）
        /// </summary>
        public AgreementTreeNode? SubLayer { get; set; }
    }
}
